package components.plugins.effects
{
	import Classes.CLoader;
	
	import SEApp.se_util;
	
	import UIUtils.uif_util;
	
	import data.clt_data;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.filters.BitmapFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class clte_textureze extends clt_baseEffect {
		private var _texture	: Bitmap;
		private var _loader		: CLoader;
		
		
		
		public function clte_textureze(a_params:Object) {
			super(uif_util.unionObj({
					textureAlpha	: 1,
					color			: [1, 1, 1],
					textureFirst	: false				
				}, a_params)
			);
		}
		
		protected function createLoader(doComplete: Function): void {
			_loader = new CLoader(null, clt_servers.DATA_URL + clt_effectLayer.TEXTURESPATH + params.id + clt_data.JPGEXT, doComplete); 
		}
		
		protected function updateBitmap(bitmapData:BitmapData):void {
			var mat		: Matrix = new Matrix();
			var bmps	: Array = [se_util.bmpCopy(_loader), copyBD(bitmapData)];
			
			mat.scale(bitmapData.width/_loader.contentLoaderInfo.width, bitmapData.height/_loader.contentLoaderInfo.height);
			var ct: ColorTransform = new ColorTransform(params.color[0], params.color[1], params.color[2], params.textureAlpha);
			
			if (params.textureFirst) {
				bitmapData.draw(bmps[1], null);
				bitmapData.draw(bmps[0], mat, ct, blendMode);
			} else {
				bitmapData.draw(bmps[0], mat);
				bitmapData.draw(bmps[1], null, ct, blendMode);
			}
			
			bmps[0].bitmapData.dispose();
			bmps[1].dispose();
		}
		
		override public function execute(bitmapData:BitmapData):void {
			if (_loader && _loader.loaded) updateBitmap(bitmapData)
			else createLoader(function(e: Event): void {
				updateBitmap(bitmapData);
			});
		}
		
		override public function incValue2(): void {
			params.textureAlpha = changeValue(params.textureAlpha, 0.1, 1);
		}
		
		override public function incValue3(): void {
			params.color[0] = changeValue(params.color[0], 0.1, 2);
			params.color[1] = changeValue(params.color[1], 0.1, 2);
			params.color[2] = changeValue(params.color[2], 0.1, 2);
		}
		
		override protected function getParamName():Array {
			return [
				'',
				locale.ALPHATEXTURE,
				locale.BRIGHT,
				locale.BLENDMODE
			];
		}
		
		override protected function getValues():Array {
			return [
				0,
				params.textureAlpha * 100,
				params.color[0]/2 * 100,
				blendMode100
			];
		}
		
		override protected function setValues(a_set:Array):void {
			params.textureAlpha = a_set[1] / 100;
			params.color[0] = a_set[2]/50;
			params.color[1] = a_set[2]/50;
			params.color[2] = a_set[2]/50;
			super.setValues(a_set);
		}
	}
}